package 延时投递;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;

import javax.jms.*;

public class Producer {

    private static final String URL = "tcp://192.168.25.133:61616";
    private static final String NAME = "queue01";

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(URL);

        activeMQConnectionFactory.setUseAsyncSend(true);

        Connection connection = activeMQConnectionFactory.createConnection();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Queue queue = session.createQueue(NAME);

        connection.start();

        MessageProducer producer = session.createProducer(queue);

        long delay = 3 * 1000;
        long period = 4 * 1000;
        int repeat = 5;

        for (int i = 1; i <= 3; i++) {
            TextMessage text = session.createTextMessage("delay message---" + i);
            text.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
            text.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
            text.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
            producer.send(text);
        }

        producer.close();
        session.close();
        connection.close();


    }
}
